﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using WFNetLib.Forms;
using WFNetLib;

namespace YouJiBiaoDing
{
    public partial class Form1 : Form
    {
        public CommunicationDebugForm CommunicationDebugForm;
        public Form1()
        {
            InitializeComponent();            
            CommunicationDebugForm = new CommunicationDebugForm();
            CommunicationDebugForm.Show();
            string[] ports = SerialPort.GetPortNames();
            Array.Sort(ports);
            cbCom.Items.Clear();
            cbCom.Items.AddRange(ports);
            cbCom.SelectedIndex = 0;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SerialCommunication com = new SerialCommunication(cbCom.Text, CommunicationDebugForm);
            ushort needLen = 27;
            byte[] RxList = new byte[27];
            com.GetRet(Convert.ToByte(numericUpDown2.Value), needLen, ref RxList);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            SerialCommunication com = new SerialCommunication(cbCom.Text, CommunicationDebugForm);
            if (!com.OpenPort(cbCom.Text))
                return;
            ushort needLen = 27;
            byte[] RxList = new byte[27];
            if (!com.GetRet(Convert.ToByte(numericUpDown2.Value), needLen, ref RxList))
                MessageBox.Show("通信失败，记录仪没有回应!!");
            com.CloseSerialPort();
            textBox12.Text = BytesOP.MakeShort(RxList[3], RxList[4]).ToString();
            textBox15.Text = BytesOP.MakeInt(RxList[5], RxList[6], RxList[7], RxList[8]).ToString();
            textBox16.Text = BytesOP.MakeShort(RxList[9], RxList[10]).ToString();
            textBox17.Text = BytesOP.MakeInt(RxList[11], RxList[12], RxList[13], RxList[14]).ToString();
            textBox18.Text = BytesOP.MakeInt(RxList[15], RxList[16], RxList[17], RxList[18]).ToString();
            textBox19.Text = BytesOP.MakeShort(RxList[19], RxList[20]).ToString();
            textBox20.Text = BytesOP.MakeShort(RxList[21], RxList[22]).ToString();
            textBox21.Text = BytesOP.MakeShort(RxList[23], RxList[24]).ToString();
            textBox22.Text = (Convert.ToInt16(textBox16.Text) * 60 + Convert.ToInt16(textBox20.Text)).ToString();
        }
    }
}
